/*
 * Copyright：fxsen <br/>
 * Date: 2021/9/22 11:31 <br/>
 * Author: <a href="mailto:fxsen@foxmail.com">fxsen</a>
 */
package com.fxsen.platform.web.controllers;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fxsen.commons.core.annotation.RecordLog;
import com.fxsen.commons.core.annotation.RequiredPermission;
import com.fxsen.commons.core.base.result.ResponseResult;
import com.fxsen.commons.core.base.result.ResultJson;
import com.fxsen.commons.core.constant.Constants;
import com.fxsen.commons.sysmanager.core.entity.Menu;
import com.fxsen.commons.sysmanager.core.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @describe: 菜单控制器 <br/>
 * @date: 2021/9/22 11:31 <br/>
 * @author: <a href="mailto:fxsen@foxmail.com">fxsen</a>
 */
@RestController
@RequestMapping("/menu")
@RequiredPermission("menu")
@Api(tags = "系统：菜单接口")
public class MenuController  {
    @Autowired
    private IMenuService menuService;
    /**
    * 保存或更新
    * @param menu 菜单模型
    * @return {@link ResultJson}
    * @date 2021/12/8 15:55
    * @author <a href="mailto:fxsen@foxmail.com">fxsen</a> <br/>
    * @version 1.0.0 <br/>
    **/
    @ApiOperation(value = "保存")
    @PostMapping("save")
    @RequiredPermission("menu:save")
    @RecordLog("新增菜单")
    public ResponseResult save(@RequestBody Menu menu, HttpServletRequest request, HttpServletResponse response){
        try {
            if (null == menu){
                return ResponseResult.failed("未传请求参数！");
            }
            if (StringUtils.isBlank(menu.getPid())){
                menu.setPid(Constants.PARENT_ID);
            }
            boolean save = menuService.save(menu);
            return save ? ResponseResult.success() : ResponseResult.saveFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "更新")
    @PostMapping("update")
    @RequiredPermission("menu:update")
    @RecordLog("更新菜单")
    public ResponseResult update(@RequestBody Menu menu, HttpServletRequest request, HttpServletResponse response){
        try {
            if (null == menu){
                return ResponseResult.failed("未传请求参数！");
            }
            if (StringUtils.isBlank(menu.getPid())){
                menu.setPid(Constants.PARENT_ID);
            }
            boolean update = menuService.updateById(menu);
            return update ? ResponseResult.success() : ResponseResult.updateFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    /**
    * 删除菜单
    * @param id 菜单主键id
    * @return {@link ResultJson}
    * @date 2021/12/8 16:14
    * @author <a href="mailto:fxsen@foxmail.com">fxsen</a> <br/>
    * @version 1.0.0 <br/>
    **/
    @ApiOperation(value = "删除")
    @GetMapping("delete")
    @RecordLog("删除菜单")
    @RequiredPermission("menu:delete")
    public ResponseResult remove(@RequestParam("id") String id, HttpServletRequest request, HttpServletResponse response){
        try {
            List<Menu> menus = menuService.findByPid(id);
            if (CollectionUtils.isNotEmpty(menus)){
                return ResponseResult.failed("已有"+ menus.size()+"个子菜单，无法删除！");
            }
            boolean remove = menuService.removeById(id);
            return remove ? ResponseResult.success() : ResponseResult.deleteFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    /**
    * 获取菜单树
    * @return {@link Map}
    * @date 2021/12/8 10:15
    * @author <a href="mailto:fxsen@foxmail.com">fxsen</a> <br/>
    * @version 1.0.0 <br/>
    **/
    @ApiOperation(value = "获取菜单树")
    @GetMapping("getTree")
    public ResponseResult getTree(String menuIds){
        try {
            List<Menu> menus = menuService.selectList();
            return ResponseResult.success(menus);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    /**
    * 获取菜单列表
    * @param
    * @return {@link Map}
    * @date 2021/12/8 10:15
    * @author <a href="mailto:fxsen@foxmail.com">fxsen</a> <br/>
    * @version 1.0.0 <br/>
    **/
    @ApiOperation(value = "获取菜单列表")
    @GetMapping("getList")
    //@RequiredPermission("menu:getList")
    public ResponseResult getList(){
        List<Menu> menus = menuService.selectList();
        return ResponseResult.success(menus);
    }
    @ApiOperation(value = "selectTree")
    @GetMapping("selectTree")
    public ResponseResult selectTree(){
        try {
            List<Menu> menus = menuService.selectList();
            return ResponseResult.success(menus);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "获取用户菜单")
    @GetMapping("getMenuByUserId")
    public ResponseResult getMenuByUserId(@RequestParam("userId") String userId){
        try {
            return ResponseResult.success(menuService.findByUserId(userId));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "获取角色菜单")
    @GetMapping("getMenuByRoleId")
    public ResponseResult getMenuByRoleId(@RequestParam("roleId") String roleId){
        try {
            return ResponseResult.success(menuService.findByRoleId(roleId));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
}
